package com.cloudsigns.factorymanager2.produce.adapter;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.project.myinerface.ItemRemoveListener;

import java.util.ArrayList;
import java.util.List;

public class ProduceEditFeedbackAdpater extends BaseAdapter {
    private Context context;
    private List<String> datas = new ArrayList<>();
    private LayoutInflater inflater;
    private String TAG = "";
    private ItemRemoveListener itemRemoveListener;
    private TextView tv_content;
    private int positionInt;

    public ProduceEditFeedbackAdpater(Context context, String tag) {
        this.context = context;
        this.inflater = LayoutInflater.from(context);
        this.TAG = tag;
    }

    public void setData(List<String> list) {
        this.datas.clear();
        if (null != list) {
            this.datas.addAll(list);
        }
    }

    public void setRemoveListener(ItemRemoveListener itemRemoveListener) {
        this.itemRemoveListener = itemRemoveListener;
    }

    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public String getItem(int position) {
        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup viewGroup) {
        final ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.item_produce_edit_feedback, null);
            holder.img_delete = convertView.findViewById(R.id.img_delete);
            holder.tv_feedback_content = convertView.findViewById(R.id.tv_feedback_content);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        //   deleteProductWorkPartPresenter = new DeleteProductWorkPartPresenter(this);

        String productBean = datas.get(position);
        holder.tv_feedback_content.setText(productBean);

        holder.img_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                View view1 = LayoutInflater.from(context).inflate(R.layout.dialog_delete, null);
                Button btn_cancle = view1.findViewById(R.id.btn_cancel);
                Button btn_sub = view1.findViewById(R.id.btn_submit);
                tv_content = view1.findViewById(R.id.tv_content);
                final Dialog dialog = new Dialog(context);
                Context context = dialog.getContext();
                try {
                    int divierId = context.getResources().getIdentifier("android:id/titleDivider", null, null);
                    View divider = dialog.findViewById(divierId);
                    divider.setBackgroundColor(Color.TRANSPARENT);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                dialog.show();
                Window window = dialog.getWindow();
                WindowManager.LayoutParams lp = window.getAttributes();
                window.setBackgroundDrawable(new ColorDrawable(0));
                window.setGravity(Gravity.CENTER);
                window.setAttributes(lp);
                dialog.setContentView(view1);
                tv_content.setText("确定删除[" + datas.get(position) + "]工序吗？");


                // 取消
                btn_cancle.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View arg0) {
                        // TODO Auto-generated method stub
                        dialog.dismiss();
                    }
                });

                // 确定
                btn_sub.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View arg0) {
                        // TODO Auto-generated method stub
                        positionInt = position;
                        //    deleteProductWorkPartPresenter.deleteProductWorkPart();
                        dialog.dismiss();
                    }
                });
            }
        });

        return convertView;
    }

    static class ViewHolder {
        ImageView img_delete;
        TextView tv_feedback_content;
    }
}
